package my.jwds.p21;

import java.util.List;

public class MergeTwoLists {
    public static void main(String[] args) {
        MergeTwoLists o = new MergeTwoLists();
        ListNode node1 = ListNode.to(new int[]{1,2,3});
        ListNode node2 = ListNode.to(new int[]{4,5,6});
        System.out.println(o.mergeTwoLists(node1,node2));
    }


    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode temp = new ListNode();
        ListNode result = temp;
        while(l1 != null && l2 != null){
            if (l1.val <= l2.val){
                temp.next = l1;
                l1 = l1.next;
            }else{
                temp.next = l2;
                l2 = l2.next;
            }
            temp = temp.next;
        }
        if (l1 != null){
            temp.next = l1;
        }
        if (l2 != null){
            temp.next = l2;
        }
        return result.next;
    }

}
